package edu.study.common.util.math;

import java.security.SecureRandom;
import java.text.NumberFormat;
import java.util.Random;

public class NumberUtil {  
  
	private static final String SYMBOLS = "0123456789"; // 数字
	 
    // 字符串
    private static final String PWD_SYMBOLS = "123456789ABCDEFGHJKLMNPQRSTUVWXYZ"; 
 
    private static final Random RANDOM = new SecureRandom();
	
	/**
	 * 获取长度为size的随机数字
	 * @return 随机数字
	 */
	public static String getNonce_str(int size) {
		
		char[] nonceChars = new char[size];
		
		for (int index = 0; index < nonceChars.length; ++index) {
			nonceChars[index] = SYMBOLS.charAt(RANDOM.nextInt(SYMBOLS.length()));
		}
		
		return new String(nonceChars);
	}
	
	/**
	 * 获取长度为size的随机字符串
	 * @return 随机字符串
	 */
	public static String getPwdStr(int size) {
		
		char[] nonceChars = new char[size];
		
		for (int index = 0; index < nonceChars.length; ++index) {
			nonceChars[index] = PWD_SYMBOLS.charAt(RANDOM.nextInt(PWD_SYMBOLS.length()));
		}
		
		return new String(nonceChars);
	}
	
	/**
	 * 获取长度为size, 左边自动填充"0"的字符串
	 * @param orderSuffix
	 * @param length
	 * @return
	 */
	public static String padLeft(int suffix, int length) {
        //得到一个NumberFormat的实例
        NumberFormat nf = NumberFormat.getInstance();
        //设置是否使用分组
        nf.setGroupingUsed(false);
        //设置最大整数位数
        nf.setMaximumIntegerDigits(length);
        //设置最小整数位数    
        nf.setMinimumIntegerDigits(length);
        //输出测试语句
        return nf.format(suffix);
	}
	
}  